package com.wang.springbootinit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.wang.springbootinit.common.ErrorCode;
import com.wang.springbootinit.constant.BISystemEnum;
import com.wang.springbootinit.exception.BusinessException;
import com.wang.springbootinit.model.entity.User;
import com.wang.springbootinit.model.param.AnswerOperateParam;
import com.wang.springbootinit.service.AnswerService;
import com.wang.springbootinit.service.UserService;
import com.wang.springbootinit.sparkAi.AiAnalyseModel;
import com.wang.springbootinit.sparkAi.AiAnalyseQueryParam;
import com.wang.springbootinit.sparkAi.AnalyseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
@Slf4j
public class AnswerServiceImpl implements AnswerService {
    @Resource
    private AnalyseService analyseService;
    @Resource
    private UserService userService;

    /**
     * 普通答疑
     * @param answerOperateParam
     * @return
     */
    @Override
    public String answerQuestion(AnswerOperateParam answerOperateParam, HttpServletRequest request) {
        if(StrUtil.isEmpty(answerOperateParam.getAnswerStr())){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(BISystemEnum.NOLOGIN.getCode(), BISystemEnum.NOLOGIN.getMsg());
        }
        AiAnalyseQueryParam aiAnalyseQueryParam = new AiAnalyseQueryParam();
        aiAnalyseQueryParam.setUserQuestionContent(answerOperateParam.getAnswerStr());
        try {
            AiAnalyseModel aiAnalyseModel = analyseService.simpleAnswerQuestion(aiAnalyseQueryParam);
            if(!aiAnalyseModel.getIsSuccess()){
                return aiAnalyseModel.getExceptionContent();
            }
            return aiAnalyseModel.getContent();
        } catch (Exception e) {
            log.error("AnswerServiceImpl answerQuestion exception:",e);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
    }
}
